package com.zmn.oms.dubbo.impl.zmn.channel.order;

import org.apache.dubbo.config.annotation.Service;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.dubbo.dto.zmn.order.ChannelPrepayAmountDIO;
import com.zmn.oms.dubbo.interfaces.zmn.channel.order.ChannelOrderModifyRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.work.modify.amount.ZsUpdateChannelPrepayAmountDTO;
import com.zmn.oms.zmn.normal.business.interfaces.work.masterwork.ZsNormalMasterWorkBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * 渠道订单修改操作
 * @author: LuJia
 * @version: v1.0
 * @since: 2020/05/20 11:43
 **/
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 3000, retries = -1, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class ChannelOrderModifyRemoteServiceImpl implements ChannelOrderModifyRemoteService {

    @Autowired
    ZsNormalMasterWorkBService zsNormalMasterWorkBService;

    @Override
    public ResponseDTO updateChannelPrepayAmount(ChannelPrepayAmountDIO channelPrepayAmountDIO) {

        String operator = channelPrepayAmountDIO.getOperator();
        Long operatorId = channelPrepayAmountDIO.getOperatorId();
        Integer operatorType = channelPrepayAmountDIO.getOperatorType();

        if (Objects.isNull(operatorType) || Objects.isNull(operatorId) || Objects.isNull(operator)) {
            return ResponseDTO.fail("操作人相关不能为空");
        }

        Integer remove = channelPrepayAmountDIO.getRemove();
        Integer channelPrepayAmount = channelPrepayAmountDIO.getChannelPrepayAmount();
        if (Objects.equals(remove, GlobalConsts.NO) && NumberUtil.isNullOrZero(channelPrepayAmount)) {
            return ResponseDTO.fail("渠道预付金额不能为空");
        }

        ZsUpdateChannelPrepayAmountDTO updateChannelPrepayAmountDTO = BeanMapper.map(channelPrepayAmountDIO, ZsUpdateChannelPrepayAmountDTO.class);
        try {
            updateChannelPrepayAmountDTO.setIsJudgeOperateAuthority(true);
            zsNormalMasterWorkBService.saveChannelPrepayAmount(updateChannelPrepayAmountDTO);
            return ResponseDTO.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseDTO.fail("修改渠道预付失败~");
        }
    }
}
